﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using WeLinkHelper.Untils.Ext;

namespace WeLinkHelper.Model
{
    public class App_service_info
    {
        /// <summary>
        /// 第三方应用标识
        /// </summary>
        public string app_service_id { get; set; }
        /// <summary>
        /// 第三方应用名称
        /// </summary>
        public string app_service_name { get; set; }
    }

    public class GroupMsgModel
    {
        /// <summary>
        /// 聊天消息发送后台应用基础信息，用户客户端界面呈现，服务端不做解析。
        /// </summary>
        public App_service_info app_service_info { get; set; }
        /// <summary>
        /// 消息标识，请求中由消息发送端输入（全局唯一，建议使用UUID）。
        /// </summary>
        public string app_msg_id { get; set; }
        /// <summary>
        /// 消息接收群ID列表。
        /// </summary>
        public List<string> group_id { get; set; }
        /// <summary>
        ///消息内容类型:
        ///0—表示纯文本
        ///10—卡片消息
        ///富媒体消息请固定写3，后台会自动分析出是文件还是图片
        ///3--富媒体图片
        ///4-富媒体文件
        /// </summary>
        public int content_type { get; set; } = 0;
        /// <summary>
        /// 消息具体内容格式
        /// <r><n>bHpoQGQ5ZDkyZTI0ZTU=</n><g>0</g><c>&lt;imbody&gt;&lt;imagelist/&gt;&lt;html&gt;&lt;![CDATA[&lt;DIV&gt;您好，欢迎使用WeLink&lt;/DIV&gt;]]&gt;&lt;/html&gt;&lt;content&gt;&lt;![CDATA[您好，欢迎使用WeLink]]&gt;&lt;/content&gt;&lt;/imbody&gt;</c></r>
        /// </summary>
        public string content { get; set; }
        /// <summary>
        /// 消息应用归属标识，固定为1
        /// </summary>
        public string client_app_id { get; set; } = "1";
        /// <summary>
        /// 此消息是否需要离线推送，默认false。
        /// </summary>
        public bool is_push { get; set; } = false;
        /// <summary>
        /// 离线push推送附加信息，可为空。如果为空，则不作离线push推送的显示加强。{“pushTitle”:””;“pushDetail”:””}
        /// </summary>
        public string push_ext_data { get; set; }
        /// <summary>
        /// 客户端消息发送时间，可用于消息链路跟踪审计；
        /// </summary>
        public int client_send_time { get; set; }
        /// <summary>
        /// 值为字符串，示例：{"senderDisplay": "1"}。senderDisplay为1时客户端发送者名称显示为app_service_info.app_service_name；为0时客户端显示为系统账号名称。未传时，客户端按照senderDisplay为0处理
        /// </summary>
        public string msg_ext { get; set; }="{\"senderDisplay\": \"1\"}";

        /// <summary>
        /// 是否在PC端展示
        /// </summary>
        [JsonIgnore]
        public string isPCDisplay { get; set; } = "1";
        /// <summary>
        /// 连接地址
        /// </summary>
         [JsonIgnore]
        public string sourceUrl { get; set; }
        /// <summary>
        /// 如果为空则使用sourceUrl
        /// </summary>
         [JsonIgnore]
        public string handlerUriAndroid { get; set; }
        /// <summary>
        /// 如果为空则使用sourceUrl
        /// </summary>
         [JsonIgnore]
        public string handlerUriIOS { get; set; }
        /// <summary>
        /// 默认49
        /// </summary>
         [JsonIgnore]
        public int? cardType { get; set; } = 49;
        /// <summary>
        /// 日期：2020年05月20日 星期三（这里是摘要）
        /// </summary>
         [JsonIgnore]
        public string digest { get; set; }
        /// <summary>
        /// OCC运营日报【05-20】（这里是标题）
        /// </summary>
         [JsonIgnore]
        public string title { get; set; }

        [JsonIgnore]
        public string enableForward { get; set; }

        [JsonIgnore]
        public string source { get; set; }

        [JsonIgnore]
        public string imgUrl { get; set; }

        /// <summary>
        /// 文件扩展
        /// </summary>
        [JsonIgnore]
        public string extention { get; set; } = ".png";


        [JsonIgnore]
        public FileInfo file { get; set; }

        /// <summary>
        /// 0:@指定人，并在要设置targetAcount，，才能在welink@我中显示
        /// 2:@所有人，并在要设置targetAcount，才能在welink@我中显示
        /// </summary>
        public int chat_at_type { get; set; }
        /// <summary>
        /// 此消息是否需要@
        /// </summary>
        public bool is_chat_at { get; set; }
        /// <summary>
        /// @人的ID
        /// </summary>
        public List<String> target_account { get; set; }




        public static string assembleCardMessage(GroupMsgModel card)
        {
            JObject cardJsonObject = new JObject();
            JObject cardContextJsonObject = new JObject();
            if (!string.IsNullOrEmpty(card.enableForward))
            {
                cardContextJsonObject.Add("enableForward", card.enableForward);
            }

            if (!string.IsNullOrEmpty(card.isPCDisplay))
            {
                cardContextJsonObject.Add("isPCDisplay", card.isPCDisplay);
            }

            if (!string.IsNullOrEmpty(card.sourceUrl))
            {
                cardContextJsonObject.Add("sourceUrl", card.sourceUrl);
            }

            if (!string.IsNullOrEmpty(card.handlerUriAndroid))
            {
                cardContextJsonObject.Add("handlerUriAndroid", escapesMsg(card.handlerUriAndroid));
            }

            if (!string.IsNullOrEmpty(card.handlerUriIOS))
            {
                cardContextJsonObject.Add("handlerUriIOS", escapesMsg(card.handlerUriIOS));
            }

            cardJsonObject.Add("cardContext", cardContextJsonObject);
            if (card.cardType!= null)
            {
                cardJsonObject.Add("cardType", card.cardType);
            }

            if (!string.IsNullOrEmpty(card.digest))
            {
                cardJsonObject.Add("digest", escapesMsg(card.digest));
            }

            if (!string.IsNullOrEmpty(card.source))
            {
                cardJsonObject.Add("source", escapesMsg(card.source));
            }

            if (!string.IsNullOrEmpty(card.imgUrl))
            {
                cardJsonObject.Add("imgUrl", escapesMsg(card.imgUrl));
            }

            if (!string.IsNullOrEmpty(card.title))
            {
                cardJsonObject.Add("title", escapesMsg(card.title));
            }

            string cardMsg = "<imbody><content><![CDATA[" + cardJsonObject.ToString() + "]]></content></imbody>";
            return spliceMessage(0, escapesMsg(cardMsg));
        }

        public static string assembleTextMessage(string content)
        {
            string msg = "<imbody><imagelist/><html><![CDATA[<DIV>" + content + "</DIV>]]></html><content><![CDATA[" + content + "]]></content></imbody>";
            return spliceMessage(0, escapesMsg(msg));
        }

        public static string assembleMediaResourceMessage(string content)
        {
            string msg = "<imbody><imagelist/><content><![CDATA[" + content + "]]></content></imbody>";
            return spliceMessage(0, escapesMsg(msg));
        }

        private static string escapesMsg(string msg)
        {
            return msg.ReplaceAll("&", "&amp;").ReplaceAll("<", "&lt;").ReplaceAll(">", "&gt;").ReplaceAll("\"", "&quot;").ReplaceAll("'", "&apos;");
        }
        private static string spliceMessage(int flag, String msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<r>");
            sb.Append("<n>");
            sb.Append("</n>");
            sb.Append("<g>");
            sb.Append(flag);
            sb.Append("</g>");
            sb.Append("<c>");
            sb.Append(msg);
            sb.Append("</c>");
            sb.Append("</r>");
            return sb.ToString();
        }
    }

    public class MediaResource
    {
        public string url { get; set; }
        public long size{ get; set; }
        public string name{ get; set; }
        public int duration{ get; set; }
        public int width{ get; set; }
        public int height{ get; set; }
        public string accessCode{ get; set; }
        public int isOriginalImg { get; set; } = 0;

        public string getExtraParam()
        {
            if (this.width <= 0 || this.height <= 0)
            {
                this.width = this.height = 0;
            }

            if (this.accessCode == null)
            {
                this.accessCode = "";
            }

            return this.width + ";" + this.height + ";" + this.accessCode;
        }

        public string toUriString()
        {
            StringBuilder builder = new StringBuilder("/:um_begin{");
            builder.Append(this.url);
            builder.Append("|");
            builder.Append("File");
            builder.Append("|");
            builder.Append(this.size);
            builder.Append("|");
            builder.Append(this.name);
            builder.Append("|");
            builder.Append(this.duration);
            builder.Append("|");
            builder.Append(this.getExtraParam());
            builder.Append("|");
            builder.Append("isOriginalImg:0");
            builder.Append("|");
            builder.Append("solidType:0");
            builder.Append("}/:um_end");
            return builder.ToString();
        }
    }
}
